package com.scuec.groupfive.controller;

import com.scuec.groupfive.service.WrongQuestionService_z;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/z")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true", maxAge = 3600)
public class WrongQuestionController_z {

    private static final Logger logger = LoggerFactory.getLogger(WrongQuestionController_z.class);

    @Autowired
    private WrongQuestionService_z wrongQuestionService_z;

    @GetMapping("/weak-points")
    public ResponseEntity<?> getWeakPoints(
            @RequestParam Long userId,
            @RequestParam(required = false) Integer subjectId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            logger.info("Received request for weak points - userId: {}, subjectId: {}, page: {}, size: {}", 
                userId, subjectId, page, size);

            if (userId == null) {
                logger.error("User ID is required");
                Map<String, String> error = new HashMap<>();
                error.put("error", "User ID is required");
                error.put("message", "用户ID不能为空");
                error.put("details", "请提供有效的用户ID");
                return ResponseEntity.badRequest().body(error);
            }

            Map<String, Object> result = wrongQuestionService_z.getWeakPointsByUserId(userId, subjectId, page, size);
            logger.info("Successfully retrieved weak points for user: {}", userId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("Error getting weak points: ", e);
            Map<String, String> error = new HashMap<>();
            error.put("error", "Internal Server Error");
            error.put("message", "获取薄弱知识点失败");
            error.put("details", e.getMessage());
            return ResponseEntity.status(500).body(error);
        }
    }
} 